﻿using System;
using System.Collections.Generic;
using System.IO;

namespace NTTDATA.BEENChina.CaseMonitor.Config
{
    [Serializable]
   public  class CommuniConfig
   {
       public List<String> CommuniPhones { get; set; }
       public string SerialPort { get; set; }
       public int BaudRate { get; set; }
       public int TimeSpan { get; set; }
       public List<String> CaseCheckerFiles { get; set; }
       


        private const String ConfigFile = "CommuniConfig.xml";

       public  static void SaveToXml(CommuniConfig communiConfig)
       {
           string filePath = AppDomain.CurrentDomain.BaseDirectory;
           //fileName = @"RunningCheckerConfig.xml";

           string fileName = Path.Combine(filePath, ConfigFile);

           if (File.Exists(fileName))
           {
               File.Delete(fileName);
           }

           using (FileStream stream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
           {
               //得到被序列化的类型
               Type type = communiConfig.GetType();
               System.Xml.Serialization.XmlSerializer sz = new System.Xml.Serialization.XmlSerializer(type);
               //开始序列化
               sz.Serialize(stream, communiConfig);
           }
       }

        public static CommuniConfig LoadCommuniConfig()
        {
            string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigFile);

            if (!System.IO.File.Exists(filePath))
            {
                throw new FileNotFoundException(filePath);
            }

            using (System.IO.StreamReader reader = new System.IO.StreamReader(filePath))
            {
                System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(CommuniConfig));
                CommuniConfig obj = xs.Deserialize(reader) as CommuniConfig;
                reader.Close();
                return obj;
            }
        }
   }
}
